
/*
 *************************************************************************
 *西瑞克斯研发管理系统
 *版 本: 1.0   
 *日 期：02/16/2012    
 *作 者： 王浩 QQ：873987060   
 *************************************************************************   
 */
package com.application.yfapplycost.dao;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.hibernate.Session;

import com.application.yfapplycost.entity.YfApplyCost;
import com.application.yfapplycost.struts.form.YfApplyCostForm;
import com.manager.commons.CommonUtil;
import com.manager.commons.dao.HibernateGenericDao;
import com.manager.commons.page.PageBean;

/**
 * 
 * Description:YfApplyCostDAO<br />
 * 
 * Version:V0.1<br />
 * 
 * Author: 王浩 QQ：873987060 <br />
 * 
 * History: <br />
 * 
 * 02/16/2012
 * 
 */
public class YfApplyCostDAO extends HibernateGenericDao<YfApplyCost> {
	private static final Logger logger = Logger.getLogger(YfApplyCostDAO.class);

	private YfApplyCost yfApplyCost;

	public YfApplyCost getYfApplyCost() {
		return this.yfApplyCost;
	}

	public void setYfApplyCost(YfApplyCost yfApplyCost) {
		this.yfApplyCost = yfApplyCost;
	}

	public Integer getAllCountYfApplyCost(YfApplyCostForm yfApplyCostForm) {
		List args = new ArrayList();
		StringBuffer sql = new StringBuffer();
		sql.append(" SELECT COUNT(0) FROM yf_apply_cost t left join yf_project p on t.project_no = p.project_no left join usertable u on t.operateid = u.id WHERE 1=1 ");
		sql = getStringBuffer(yfApplyCostForm, sql, args);
		return super.getCount(sql.toString(), args.toArray());
	}
	
	public  String getMaxNo(String pix ){
		String temp = "ABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZ";
		StringBuffer buf = new StringBuffer();
		buf.append("select max(APPLY_NO) maxNo from yf_apply_cost WHERE APPLY_NO like  '%" + pix + "%' ");
		Map projectNoMap = getJdbcTemplate().queryForMap(buf.toString());
		String maxNo = (String) projectNoMap.get("maxNo");
        if(StringUtils.isBlank(maxNo)){
        	return pix + "A";
        }
        maxNo = maxNo.substring(11);
        int current = temp.indexOf(maxNo);
        maxNo = temp.substring(current+1,current+2);
		return pix + maxNo;
	}

	public void deleteYfApplyCosts(YfApplyCostForm yfApplyCostForm) {
		String hql = " delete from YfApplyCost where id ='"+ yfApplyCostForm.getId() + "'";
		Session session = this.getSession();
		try {
			session.createQuery(hql).executeUpdate();
		} catch (RuntimeException re) {
			logger.error("deleteYfApplyCosts failed");
			throw re;
		} finally {
			if (session != null) {
				releaseSession(session);
			}
		}
	}
    
	public List searchYfApplyCost(YfApplyCostForm yfApplyCostForm,
			PageBean pageBean) {
		List args = new ArrayList();
		StringBuffer sql = new StringBuffer();
		sql
				.append(" SELECT t.*,u.rname rname,p.* FROM yf_apply_cost t left join yf_project p on t.project_no = p.project_no left join usertable u on t.operateid = u.id WHERE 1=1");
		sql = getStringBuffer(yfApplyCostForm, sql, args);
		sql.append(" order by t.create_date desc");
		return getPageInfo(pageBean, sql.toString(), args);
	}

	/**
	 * 查询条件的封装
	 * 
	 * @param departmentForm
	 * @return
	 */
	private StringBuffer getStringBuffer(YfApplyCostForm yfApplyCostForm,
			StringBuffer buf, List args) {
		/*
		 * 需要加入查询条件时封装
		 */

		if (StringUtils.isNotBlank(yfApplyCostForm.getProjectNo())) {
			buf.append(" and t.PROJECT_NO like ? ");
			args.add("%" + yfApplyCostForm.getProjectNo().trim() + "%");
		}
		if (StringUtils.isNotBlank(yfApplyCostForm.getApplyNo())) {
			buf.append(" and t.APPLY_NO like ? ");
			args.add("%" + yfApplyCostForm.getApplyNo().trim() + "%");
		}

		if (null != yfApplyCostForm.getAuditStatus()
				&& yfApplyCostForm.getAuditStatus().longValue() != 0) {
			buf.append(" and t.AUDIT_STATUS = ? ");
			args.add(yfApplyCostForm.getAuditStatus());
		}

		if (StringUtils.isNotBlank(yfApplyCostForm.getOrg())) {
			buf.append(" and t.ORG = ? ");
			args.add(yfApplyCostForm.getOrg().trim());
		}
		if (StringUtils.isNotBlank(yfApplyCostForm.getOperateid())) {
			buf.append(" and t.OPERATEID=? ");
			args.add(yfApplyCostForm.getOperateid().trim());
		}
		if (StringUtils.isNotBlank(yfApplyCostForm.getId())) {
			buf.append(" and t.id = ? ");
			args.add(yfApplyCostForm.getId());
		}
		if (StringUtils.isNotBlank(yfApplyCostForm.getProjectName())) {
			buf.append(" and p.project_name like ? ");
			args.add("%" + yfApplyCostForm.getProjectName().trim() + "%");
		}
		if (StringUtils.isNotBlank(yfApplyCostForm.getProjectOrg())) {
			buf.append(" and p.project_org = ? ");
			args.add(yfApplyCostForm.getProjectOrg());
		}
		if (null != yfApplyCostForm.getProjectType()
				&& yfApplyCostForm.getProjectType().longValue() != 0) {
			buf.append(" and p.project_type = ? ");
			args.add(yfApplyCostForm.getProjectType());
		}
		if (StringUtils.isBlank(yfApplyCostForm.getId())) {
			buf = CommonUtil.filterDataFee(buf,args);
		}
		
		return buf;
	}

}
